package IC.Parser;

/**
 * A class that represents a syntax error, will be thrown by the parser
 * whenever a syntax error will occur.
 * @author nimrod
 *
 */
public class SyntaxError extends Exception {
	
	private static final long serialVersionUID = 1034509028547013128L;
	
	private int line;
	private Token token;
	
	/**
	 *  Constructor for SyntaxError.
	 * @param message 
	 * 		The exception's message.
	 */
	public SyntaxError(String message, int line, Token token) {
    	super(message);
    	this.line = line;
    	this.token = token;
    }
	
	/**
     *  getter for line field
     * @return 
     * 			Returns the line of the token that caused the exception.
     */
	public int getLine() {
    	return this.line;
    }
	
	/**
	 * getter for the token that caused the syntax error.
	 * @return 
	 * 		Returns the token that caused the syntax error.
	 */
	public Token getToken() {
    	return this.token;
    }
    
	/** Returns a string representation of the class
     * @return 
     * 		Returns a string representation of the class.
     */
    public String toString() {
    	String msg = this.getMessage();
    	String append = "";
    	
    	if (token != null) {
    		append += " at token '" + token.toString() + "'";
    	}
    	
    	if (msg != null && msg.length() != 0) {
    		append += " " + this.getMessage();
    	}
    	
    	return String.valueOf(line) + ": Syntax error: " + append;
    }
}
